package com.jml.队列和线程池;

import org.junit.Test;

import java.util.concurrent.*;

public class QueueTest {

	@Test
	public  void concurrentLinkedQueueTest() {
		ConcurrentLinkedQueue<String> concurrentLinkedQueue=new ConcurrentLinkedQueue();
		concurrentLinkedQueue.offer("1");
		concurrentLinkedQueue.offer("2");
		concurrentLinkedQueue.offer("3");
		System.out.println(concurrentLinkedQueue.size());
		String poll = concurrentLinkedQueue.poll();
		System.out.println(poll);
		System.out.println(concurrentLinkedQueue.size());
	}

	@Test  //
	public void arrayBlockQueueTest() throws InterruptedException {
		BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(3);
		blockingQueue.offer("1q");
		//存入如果队列满了，才会等待，超时就会放弃存入
		// 如果没有时间参数就直接放弃
		blockingQueue.offer("2q",3,TimeUnit.SECONDS);
		blockingQueue.offer("3q",7,TimeUnit.SECONDS);
		blockingQueue.offer("4q",1,TimeUnit.SECONDS);
		//如果队列为空，才会等待，等待超时就会返回null
		//如果没有时间参数就直接返回null
		System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
		System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
		System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
		System.out.println(blockingQueue.poll());
	}

}
